<?php



/**
 * Skeleton subclass for representing a row from the 'games' table.
 *
 *
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package    propel.generator.nawragon
 */
class Games extends BaseGames
{
	
	//TODO: przenie�� sta�e do configa
	const NUM_OF_PROVINCES = 7;
	
	/**
	 * Metoda zwraca losowe wpisy spo�r�d danej kolekcji.
	 * @param PropelObjectCollection $input
	 * @param unknown $amount
	 * @return PropelObjectCollection
	 */
	private function getRandomEntries(PropelObjectCollection $input, $amount) {
		$array = $input->getArrayCopy();
		$keys = array_rand($array, $amount);
		$result = new PropelObjectCollection();
		foreach ($keys as $key)
			$result->append($input->get($key));
		return $result;
	}
	
	/**
	 * Tworzenie nowej gry.
	 * @param int[] $userIds tablica zawieraj�ca numery id urzytkownik�w bior�cych dzia� w nowej grze
	 * @param int $typeId okre�la typ gry
	 */
	public function fillGame($userIds, $typeId) {
		$numOfPlayers = count($userIds);
	
		$characters = CharactersQuery::create()->find();
		$characters = $this->getRandomEntries($characters, $numOfPlayers);
	
		$stateDefs = StateDefsQuery::create()->leftJoinProvinceDefs('pd')->with('pd')->find();
		$stateDefs = $this->getRandomEntries($stateDefs, $numOfPlayers);
	
		$materialDefs = MaterialDefsQuery::create()->find();
		$armyDefs = ArmyDefsQuery::create()->find();
		$infrastructureDefs = InfrastructureDefsQuery::create()->find();
	
		foreach ($userIds as $userId) {
			$player = new Players();
			$player->setActive(1);
			$player->setUserId($userId);
			$player->setCharacters($characters->pop());
			$player->setStateDefs($stateDefs->pop());
				
			$provinceDefs = $player->getStateDefs()->getProvinceDefss();
			$provinceDefs = $this->getRandomEntries($provinceDefs, self::NUM_OF_PROVINCES);
				
			foreach ($provinceDefs as $provinceDef) {
				$province = new Provinces();
				$province->setActive(1);
				$province->setProvinceDefs($provinceDef);
				foreach ($materialDefs as $materialDef) {
					$material = new Materials();
					$material->setMaterialDefs($materialDef);
					$material->setValue(7500); //TODO: zaimplementowa� algorytm wyznaczaj�cy liczb� surowc�w.
					$province->addMaterials($material);
				}
				foreach ($armyDefs as $armyDef) {
	
					$army = new Army();
	
					$army->setArmyDefs($armyDef);
	
					$army->setCount(100); //TODO: zaimplementować algorytm wyznaczający liczbę jednostek.
	
					$province->addArmy($army);
	
				}
				foreach ($infrastructureDefs as $infrastructureDef) {
	
					$infrastructure = new Infrastructures();
	
					$infrastructure->setInfrastructureDefs($infrastructureDef);
	
					$infrastructure->setLevel(0);
	
					$province->addInfrastructures($infrastructure);
	
				}
				$player->addProvinces($province);
			}
			$this->addPlayers($player);
			$round = new Rounds();
			$round->setName('test');
			$round->setStart(new DateTime('NOW'));
			$round->setEnd(new DateTime('NOW'));
			$this->addRounds($round);
			$this->setType($typeId);
			$this->setActive(1);
		}
		$this->save();
	}
	
	public function __construct($userIds, $type) {
		parent::__construct();
		if ($userIds !== null && $type !== null) {
			$this->fillGame($userIds, $type);
		}
	}
	
}
